package com.eshop.web.controllers.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.eshop.common.constant.CoreConstant;
import com.eshop.frameworks.core.controller.BaseController;
import com.eshop.model.mongodb.EUser;
import com.eshop.model.mongodb.EUserAddress;
import com.eshop.service.mongodb.EUserAddressService;

@Controller
@RequestMapping("/address")
public class AddresController extends BaseController {
	private static final Logger logger = Logger.getLogger(EUserController.class);
	
	@Autowired
	private EUserAddressService euserAddressService;

	@RequestMapping("")
	public ModelAndView address(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/euser/address.httl");
		setVar(mav);
		EUser user = (EUser) this.getSessionAttribute(request,
				CoreConstant.USER_SESSION_NAME);
		if (user == null) {
			mav.setViewName("redirect:/login");
			return mav;
		}
		List<EUserAddress> addressList = euserAddressService
				.getAddressByUserId(user.getId());
		mav.addObject("user", user);
		mav.addObject("addressList", addressList);
		mav.addObject("leaf", "address");
		return mav;
	}
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveAddress(EUserAddress euserAddress,@RequestParam(value="url",required=false,defaultValue="address") String url,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView("redirect:/"+url);
		setVar(modelAndView);
		try {
			EUser user = (EUser) this.getSessionAttribute(request,
					CoreConstant.USER_SESSION_NAME);
			if (user == null) {
				modelAndView.setViewName("redirect:/login");
				return modelAndView;
			}
			euserAddress.setUserId(user.getId());
			euserAddressService.save(euserAddress);
		} catch (Exception e) {
			logger.error("EUserController.saveAddress", e);
		}
		return modelAndView;
	}

//	@RequestMapping(value = "/subAddress", method = RequestMethod.POST)
//	public ModelAndView subAddress(EUserAddress euserAddress,
//			HttpServletRequest request, HttpServletResponse response) {
//		ModelAndView modelAndView = new ModelAndView("redirect:/euser/"+url);
//		setVar(modelAndView);
//		try {
//			EUser user = (EUser) this.getSessionAttribute(request,
//					CoreConstant.USER_SESSION_NAME);
//			if (user == null) {
//				modelAndView.setViewName("redirect:/login");
//				return modelAndView;
//			}
//			euserAddress.setUserId(user.getId());
//			euserAddressService.save(euserAddress);
//		} catch (Exception e) {
//			logger.error("EUserController.saveAddress", e);
//		}
//		return modelAndView;
//	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ModelAndView delAddress(@RequestParam(value="addressId",required=true) String addressId,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView("redirect:/address");
		setVar(modelAndView);
		try {
			EUser user = (EUser) this.getSessionAttribute(request,
					CoreConstant.USER_SESSION_NAME);
			if (user == null) {
				modelAndView.setViewName("redirect:/login");
				return modelAndView;
			}
			euserAddressService.removeById(addressId, EUserAddress.class);
		} catch (Exception e) {
			logger.error("EUserController.delAddress", e);
		}
		return modelAndView;
	}
	
	/**
	 * 设置默认地址
	 * @param addressId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/setdef", method = RequestMethod.POST)
	public ModelAndView setDefault(@RequestParam(value="addressId",required=true) String addressId,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView = new ModelAndView("redirect:/address");
		setVar(modelAndView);
		try {
			EUser user = (EUser) this.getSessionAttribute(request,
					CoreConstant.USER_SESSION_NAME);
			if (user == null) {
				modelAndView.setViewName("redirect:/login");
				return modelAndView;
			}
			euserAddressService.setDefault(addressId, user.getId());
		} catch (Exception e) {
			logger.error("EUserController.setDefault", e);
		}
		return modelAndView;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/myDef", method = RequestMethod.POST)
	public EUserAddress getDefaultAddress(@RequestBody EUser user,
			HttpServletRequest request, HttpServletResponse response) {
		EUserAddress address = null;
		try {
			address = euserAddressService.getMyDefaultAddress(user.getId());
			if(address==null){
				List<EUserAddress> addressList = euserAddressService.getAddressByUserId(user.getId());
				if(addressList.size()>=1){
					address = addressList.get(0);
				}
			}
		} catch (Exception e) {
			logger.error("EUserController.saveShipping", e);
		}
		return address;
	}
}
